﻿

import Request from '../../base/request.js?t=202511041006'
import Storage from '../../base/storage.js?t=202511041006'
import { dealTime, IgetDeptList, getRecentYears, getMonthList, getBrowserInfo } from "../../base/utils.js?t=202511041006"
const Composition = (proxy) => {

    const data = reactive({
        messageTest: "",
        proxy: "",
        popTitle:"",
        cIndex: 0,
        tabList: [{ label: "年度考核", value: 0 }, { label: "月度考核", value: 1 }],
        queryParams: {
            year: null,
            month: null,
        },

        userinfo: {},
        countSetting: {
            show: false,
            time: 5,
            otime: 5,
            osize:26,
            size:26
        },

        planId: null,
        isAllPass:false,
        orecord:null,
        detailQueryParams: {
            dept: null,
            type: null,
            //status: null,
        },

        Ids:[],
        plform: {

        },
        statusList:[],
        opType:true,
        popShow:false,
        pagination: {
            total: 0,
            current: 1,
            pageSize: 10,
            loading:true,
            showTotal: total => `总共 ${total} 项`,
            defaultPageSize: 10,
            // pageSizeOptions: ['5', '10', '15', '20'], // 可不设置使用默认
            showSizeChanger: true, // 是否显示pagesize选择
            showQuickJumper: false, // 是否显示跳转窗
        },
        detailPagination: {
            total: 0,
            current: 1,
            pageSize: 10,
            loading: true,
            showTotal: total => `总共 ${total} 项`,
            defaultPageSize: 10,
            // pageSizeOptions: ['5', '10', '15', '20'], // 可不设置使用默认
            showSizeChanger: true, // 是否显示pagesize选择
            showQuickJumper: false, // 是否显示跳转窗
        },
        deptList: [
  
        ],

        yearList:[
         
        ],
        monthList: [
   
        ],
        detailColumns: [
            {
                title: '序号',
                dataIndex: 'a',
                key: 'a',
                align: 'center',
                width: 80,
                customRender: ({ index }) => {
                    return (params.detailPagination.value.current - 1) * params.detailPagination.value.pageSize + index + 1
                },
            },

            {
                title: '部门',
                dataIndex: 'DepartmentName',
                key: 'DepartmentName',
                align: 'center',

            },
            {
                title: '部门类型',
                dataIndex: 'DepartmentTypeName',
                key: 'DepartmentTypeName',
                align: 'center',
                customRender: ({ text }) => {
                    return text 
                },
            },
            {
                title: '自评分',
                dataIndex: 'SelfTotalScore',
                key: 'SelfTotalScore',
                align: 'center',
                sorter: (a, b) => {
                    return a.SelfTotalScore - b.SelfTotalScore
                },
            },
            {
                title: '考评分(百分制)',
                dataIndex: 'TotalScore',
                key: 'TotalScore',
                align: 'center',
                customRender: ({ text }) => {
                    return text||'-'
                },
                sorter: (a, b) => {
                    return a.TotalScore - b.TotalScore
                },
            },
            {
                title: '填报状态',
                dataIndex: 'StatusName',
                key: 'StatusName',
                align: 'center',
                customRender: function ({ text }) {
                    return h(antd.Tag, {
                        color: "#108ee9",
                    }, { default: () => text });
                }
            },
            {
                title: '考评人',
                dataIndex: 'EvalutionUserList',
                key: 'EvalutionUserList',
                align: 'center',
                customRender: function ({ text }) {
        return text || '-'
                }
            },
            

            {
                title: '操作',
                key: 'action',
                align: 'center',
                customRender: function ({  record, }) {

                
                    let approvalBtn  = h(antd.Button, {

                        type: "link",
                        onClick: () => methods.handleApproval(record.Id, record.PlanId, record.Status)
                    }, { default: () => '批准' })


                    let checkBtn = h(antd.Button, {
                     
                        type: "link",
                        onClick: () => methods.handleCheck(record.Id,record.PlanId)
                    }, { default: () => '考评' })


                    let detailBtn = h(antd.Button, {
                        type: "link",
                        onClick: () => methods.handleIFillDetail(record,record.PlanId, record.Id)
                    }, { default: () => '详情' })

 
                    return h(antd.Space, {
                    },
                        { default: () => [record.IsEvalution ? checkBtn : null, record.IsRatify ? approvalBtn : null, detailBtn] }
                    );

                }
            },
        ],
        detailSource:[],
        DepartmentTypeList:[],
        columns1: [
            {
                title: '序号',
                dataIndex: 'a',
                key: 'a',
                align: 'center',
                width: 80,
                customRender: ({ index}) => {
                    return (params.pagination.value.current - 1) * params.pagination.value.pageSize + index + 1
                },
            },

            {
                title: '年份',
                dataIndex: 'Years',
                key: 'Years',
                align: 'center',

            },
            {
                title: '考核月份',
                dataIndex: 'Months',
                key: 'Months',
                align: 'center',
                customRender: ({ text}) => {
                    return text+ '月'
                },
            },
            {
                title: '部门',
                dataIndex: 'DepartmentName',
                key: 'DepartmentName',
                align: 'center'
            },
            {
                title: '分值',
                dataIndex: 'TotalScore',
                key: 'TotalScore',
                align: 'center',
                sorter: (a, b) => {
                    return a.TotalScore - b.TotalScore
                },
                customRender: function ({ text }) {
                    return (Math.floor(+text * Math.pow(10, 2)) / Math.pow(10, 2)).toFixed(2)
                }
            },
            {
                title: '日期',
                dataIndex: 'UpdateTime',
                key: 'UpdateTime',
                align: 'center',
                customRender: function ({ text }) {
                    return dealTime(text,'YYYY-MM-DD')
                }
            },
            //{
            //    title: '状态',
            //    dataIndex: 'StatusName',
            //    key: 'StatusName',
            //    align: 'center',
            //    customRender: function ({ text, record, index, column }) {   
            //        return h(antd.Tag, {
            //            color: text == '保存' ? "#87d068" : "#108ee9",
            //        }, { default: () => text });
            //    }
            //},
            //{
            //    title: '审核人数量',
            //    dataIndex: 'ApproverCount',
            //    key: 'ApproverCount',
            //    align: 'center',

            //    customRender: function ({ text, record, index, column }) {
       

            //        let content = h('div', {
            //            style: 'width:700px'
            //        }, { default: () => record.ApproverCount.map(e => e).join('、 ') });


            //        let textc = h(antd.Button, {
            //            type: "link"
            //        }, { default: () => record.ApproverCount.length });

            //        return record.ApproverCount.length == 0 ? h(antd.Space, {
            //        }, () => [textc]): h(antd.Popover, {
            //            title: '审核人姓名',
            //            content: content,

            //        }, () => [textc]);
            //    }


        
            //},
            {
                title: '操作',
                key: 'action',
                align: 'center',
                customRender: function ({ text, record, index, column }) {

                    let editBtn = h(antd.Button, {
                        type: "link",
                        onClick: () => methods.handleMEdit(record.Id,true)
                    }, { default: () => '编辑' })


                    let returnBtn = h(antd.Button, {
                  
                        type: "link",
  
                    }, { default: () => '撤回' })
                    let detailBtn = h(antd.Button, {
       
                        type: "link",
                        onClick: () => methods.handleMEdit(record.Id,false)
                    }, { default: () => '详情' })

                    //let publicBtn = h(antd.Button, {
                    //    style: record.Status == 0 ? '' : 'display:none',
                        
                    //    type: "link"
                    //}, { default: () => '提交' })

                    //const fPublicBtn = h(antd.Popconfirm, {
                    //    title: "确定提交此数据？",
                    //    onConfirm: () => methods.handlePublick(record.Id)

                    //}, { default: () => publicBtn })


                    const fReturnBtn = h(antd.Popconfirm, {
                        title: "确定撤回此数据？",
                        onConfirm: () => methods.handleMReturn(record.Id)

                    }, { default: () => returnBtn })


                    let delBtn = h(antd.Button, {
                        style: 'margin-right:4px',
                        type: "link",
                        danger:true
                    }, { default: () => '删除' })

                    const fDelBtn = h(antd.Popconfirm, {
                        title: `提示：通知标题【${record.Title}】，删除后将无法恢复，请您确认。`,
                        onConfirm: () => methods.handleMDelete(record.Id)

                    }, { default: () => delBtn })


                    return h('div', {
                    },
                        { default: () => [record.IsEdit ? editBtn : null, record.IsWithDraw ? fReturnBtn : null, detailBtn, record.IsDelete? fDelBtn:null] }
                    );

                }
            },
        ],
        columns2: [
            {
                title: '序号',
                dataIndex: 'a',
                key: 'a',
                align: 'center',
                width: 80,
                customRender: ({  index }) => {
                    return (params.pagination.value.current - 1) * params.pagination.value.pageSize + index + 1
                },
            },
            {
                title: '通知标题',
                dataIndex: 'Title',
                key: 'Title',
                align: 'center'

            },
            {
                title: '考核日期',
                dataIndex: 'UpdateTime',
                key: 'UpdateTime',
                align: 'center',
                customRender: ({ record }) => {
                    return `${dealTime(record.StartTime)}~${dealTime(record.EndTime)}`
                },
            },
            {
                title: '部门',
                dataIndex: 'DepartmentName',
                key: 'DepartmentName',
                align: 'center'
            },
            {
                title: '部门类型',
                dataIndex: 'DepartmentTypeName',
                key: 'DepartmentTypeName',
                align: 'center',
         
            }, 
            {
                title: '自评总分',
                dataIndex: 'SelfScore',
                key: 'SelfScore',
                align: 'center',
                customRender: function ({ text, record }) {

   
                    return Number.parseFloat(text)?text:'-'
                }
     
            },
            {
                title: '考评总分',
                dataIndex: 'TotalScore',
                key: 'TotalScore',
                align: 'center',
                customRender: function ({ text, record }) {
                    return Number.parseFloat(text) ? text : '-'
                }
          
            },
            {
                title: '状态',
                dataIndex: 'StatusName',
                key: 'StatusName',
                align: 'center',
                customRender: function ({ text, record }) {
                    let steps = h(antd.Steps, {
                        type: "inline",
                        current: params.statusList.value.findIndex((e) => {
                            return e.label == record.StatusName
                        }),
                        items: params.statusList.value.map(e => {

                            return {
                                key: e.label,
                                title: e.label,
                                description: e.label
                            }
                        }),
                    }, { default: () => text });
                    let textc = h(antd.Tag, {
                        color: "#2178e9" ,
                        style: 'cursor:pointer'
                    }, { default: () => text });

                    return h(antd.Popover, {
                        title: '当前节点',
                        content: steps,
                    }, () => [textc]);

                }
            },
            //{
            //    title: '备注',
            //    dataIndex: 'Remark',
            //    key: 'Remark',
            //    align: 'center'
            //},
            {
                title: '操作',
                key: 'action',
                align: 'center',
                customRender: function ({ text, record, index, column }) {

               

                   let fillBtn = h(antd.Button, {

                        type: "link",
                       onClick: () => {
                           methods.handleFill(record.Id, record.FormId)
                        }
                   }, { default: () => record.FormId ? '填报' :'填报' })





                    const fwithDrawbtn = h(antd.Popconfirm, {
                        title: "确定撤回此数据？",
                        onConfirm: () => methods.handlewithDraw(record.FormId)

                    }, { default: () => withDrawbtn })


                    let withDrawbtn = h(antd.Button, {

                        type: "link",
       
                    }, { default: () => '撤回' })

                    let fillRecordBtn = h(antd.Button, {

                        type: "link",
                        onClick: () => {
                            params.orecord.value = record
                            methods.handleFillRecord(params.orecord.value||record)
                        }
                    }, { default: () => '考评' })


                    let detailBtn = h(antd.Button, {

                        type: "link",
                        onClick: () => {
                            methods.handleFillDetail(record,record.Id, record.FormId)
                        }
                    }, { default: () => '详情' })


                    return h(antd.Space, {
                    },
                        { default: () => [record.IsShowFill ? fillBtn : null, record.IsShowWithdraw ? fwithDrawbtn : null, record.IsShowRecord ? fillRecordBtn : null, record.FormId ? detailBtn : null] }
                    );

                }
            },
        ],
        columns3: [
            {
                title: '序号',
                dataIndex: 'a',
                key: 'a',
                align: 'center',
                width: 80,
                customRender: ({ index}) => {
                    return (params.pagination.value.current - 1) * params.pagination.value.pageSize + index + 1
                },
            },
            {
                title: '年度',
                dataIndex: 'a',
                key: 'a',
                align: 'center'

            },
            {
                title: '部门',
                dataIndex: 'b',
                key: 'b',
                align: 'center'
            },
            {
                title: '月度总分值',
                dataIndex: 'c',
                key: 'c',
                align: 'center'
            },
            {
                title: '年度总分值',
                dataIndex: 'd',
                key: 'd',
                align: 'center'
            },
            {
                title: '实际总分值',
                dataIndex: 'e',
                key: 'e',
                align: 'center',
                sorter: (a, b) => a.e - b.e,
            },
 
        ],

        dataSource1: [],
        dataSource2: [],
        dataSource3: [],
    })
    let params = toRefs(data)
    const methods = {
        tchange: (e) => {
            console.log(params.cIndex.value, 'params.cIndex.value')
            Storage.set(`${window.location.pathname}Index`, params.cIndex.value, true)
            params.queryParams.value = {
            }
            params.queryParams.value.LevelId = params.cIndex.value
            methods.getTableList(true)

        },
        handleReset: () => {
            params.queryParams.value = {

            }
            methods.getTableList(true)
        },
        handleMEdit: (id,flag) => {
            if (flag) {
                window.location.href = `/Performance/PerformanceMonthAdd?id=${id}&type=1`
            } else {
                window.location.href = `/Performance/PerformanceMonthAdd?id=${id}&type=9`
            }
        },
        handleMReturn: (id) => {
            params.pagination.value.loading = true
            Request({
                url: "/performance/CallbackMonthReport",
                method: 'post',
                data: {
                    Id: id
                }
            }).then(res => {
                params.messageTest.value.info(res.Msg);
                methods.getTableList(true)
            }).finally(() => {
                params.pagination.value.loading = false
            })
        },
        handleMDelete: (id) => {
            params.pagination.value.loading = true
            Request({
                url: "/performance/DeleteMonthReportInfo",
                method: 'post',
                data: {
                    id: id
                }
            }).then(res => {
                params.messageTest.value.info(res.Msg);
                methods.getTableList(true)
            }).finally(() => {
                params.pagination.value.loading = false
            })
        },
        handleOk: () => {
            params.popShow.value = false

        },
        handleCounSetting: () => {
            window.localStorage.setItem('pl', params.countSetting.value.time)
            window.localStorage.setItem('size', params.countSetting.value.size)
            params.messageTest.value.info('保存成功');
            params.countSetting.value.show = false
        },
        handlewithDraw: (id) => {
            params.pagination.value.loading = true
            Request({
                url: "/performance/WithDrawYearsReportForm",
                method: 'post',
                data: {
                    FormId: id
                }
            }).then(res => {
                params.messageTest.value.info(res.Msg);
                methods.getTableList(true)
            }).finally(() => {
                params.pagination.value.loading = false
            })


        },
        GetDepartmentList: async () => {
            let res = await IgetDeptList(Request)
            params.deptList.value = res
        },
        GetStatusList: () => {
            Request({
                url: "/performance/GetStatusList",
                method: 'post',

            }).then(res => {
                params.statusList.value = res.Data.map(e => {
                    return {
                        label: e.OperateName,
                        value: e.StatusId
                    }
                })
            })
        },



        GetDepartmentTypeList: () => {
            Request({
                url: "/performance/GetDepartmentTypeList",
                method: 'post',

            }).then(res => {
                params.DepartmentTypeList.value = res.Data.map(e => {
                    return {
                        label: e.TypeName,
                        value: e.Id
                    }
                })
            })
        },
        GetDepartmentBookListByPlan: () => {
            params.detailPagination.value.loading = true

            Request({
                url: "/performance/GetDepartmentBookListByPlan",
                method: 'post',
                params: {


                    DepartmentId: params.detailQueryParams.value.dept,
                    DepartmentTypeId: params.detailQueryParams.value.type,
                    PlanId: params.planId.value,

                },
            }).then(res => {
                params.detailSource.value = res.Data
                if (res.Data.length != 0) {
                    params.isAllPass.value = res.Data.filter(e => e.Status == 3).map(e => e.IsRatify).some(e => e == true)


                }

            }).finally(() => {
                params.detailPagination.value.loading = false
            })
        },
        plSubmit: (status) => {
            params.opType.value = status
            params.popShow.value = true
        },
        allpass: () => {
            Request({
                url: "/performance/ApproveYearReportList",
                method: 'post',
                data: {
                    list: params.Ids.value
                },
            }).then(res => {
                params.messageTest.value.info(res.Msg);
                methods.GetDepartmentBookListByPlan()
            })
        },
        rowSelection: {
            onChange: (selectedRowKeys, selectedRows) => {
                params.Ids.value = selectedRowKeys
            },
            getCheckboxProps: record => ({
                disabled: record.Status != 3

            })
        },

        handleFill: (id, fid) => {

            if (fid) {
                window.open(`/Performance/PerformanceMeansDetail?optype=0&id=${id}&fid=${fid}`, "_blank")

            } else {
                window.open(`/Performance/PerformanceAddAssess?optype=0&id=${id}`, "_blank")

            }

        },

        handleFillDetail: (record, id, fid) => {

            window.open(`/Performance/PerformanceMeansDetail?optype=1&id=${id}&fid=${fid}`, "_blank")


        },
        handleIFillDetail: (record, id, fid) => {

            switch (+record.Status) {
                case 2:
                    window.open(`/Performance/PerformanceMeansEvaluate?id=${record.Id}&planid=${record.PlanId}&detail=1`, "_blank")

                    break;
                case 3:
                    window.open(`/Performance/PerformanceMeansCheck?id=${record.Id}&planid=${record.PlanId}&status=${record.Status}&detail=1`, "_blank")

                    break;
                case 4:
                    window.open(`/Performance/PerformanceMeansCheck?id=${record.Id}&planid=${record.PlanId}&status=${record.Status}&detail=1`, "_blank")

                    break;

                default:

                    window.open(`/Performance/PerformanceMeansDetail?optype=1&id=${id}&fid=${fid}`, "_blank")

                    break;
            }


        },



        handleFillRecord: (record) => {
            params.planId.value = record.Id
            params.popTitle.value = `${record.Title}各部门填报情况`
            methods.GetDepartmentBookListByPlan()
            params.popShow.value = true

        },

        filterOption: (val, type) => {


            return `${type.label}`.includes(`${val || ''}`)
        },


        Cdetail: () => {
            methods.GetDepartmentBookListByPlan()
        },
        handleMonthAdd: () => {
            window.location.href = '/Performance/PerformanceMonthAdd'
        },
        fillInfo: () => {
            if (params.cIndex.value == 0) {
                //年度填报
                window.location.href = '/Performance/PerformanceAddAssess?type=0'
            }
            else {
                //月度填报

            }
        },
        handleTableChange: (page) => {
            const { current, pageSize } = page
            params.pagination.value.current = current;
            params.pagination.value.pageSize = pageSize;
            methods.getTableList()
        },

        handleDetailTableChange: (page) => {
            const { current, pageSize } = page
            params.detailPagination.value.current = current;
            params.detailPagination.value.pageSize = pageSize;
            methods.GetDepartmentBookListByPlan()
        },

        handleCheck: (id, pid) => {
            window.open(`/Performance/PerformanceMeansEvaluate?id=${id}&planid=${pid}`, '_blank')
        },
        handleApproval: (id, pid, status) => {
            window.open(`/Performance/PerformanceMeansCheck?id=${id}&planid=${pid}&status=${status}`, '_blank')

        },

        handlekhDetail: (id) => {
            window.open(`/Performance/PerformanceMeansDetail?id=${id}`, '_blank')
        },


  
        getTableList: async(isRresh) => {

            if (isRresh) {
                params.pagination.value.current = 1
            }
            params.pagination.value.loading = true

            //月度考核
            if (params.cIndex.value == 1) {
                Request({
                    url: "/Performance/GetMonthReportPageList",
                    method: 'post',
                    params: {
                        page: params.pagination.value.current,
                        pageSize: params.pagination.value.pageSize
                    },
                    data: {
                        "DepartmentId": params.queryParams.value.dept,
                        "Years": params.queryParams.value.year,
                        "Month": params.queryParams.value.month
                    }
                }).then(res => {
                    params.dataSource1.value = res.Data.Data
                    params.pagination.value.total = res.Data.TotalItemCount
                   
                }).finally(() => {
                    params.pagination.value.loading = false
                })
            }
                        //年度考核
            if (params.cIndex.value == 0) {
                Request({
                    url: "/performance/GetMyPerformanceTaskPageList",
                    method: 'post',
                    params: {
                        page: params.pagination.value.current,
                        pageSize: params.pagination.value.pageSize
                    },
                    data: {
                        "Years": params.queryParams.value.year,
                        "Month": params.queryParams.value.month
                    }
                }).then(res => {
                    params.dataSource2.value = res.Data?.Data
                    params.pagination.value.total = res.Data?.TotalItemCount
                }).finally(() => {
                    params.pagination.value.loading = false
                })

            }




        },


        init: () => {
              getBrowserInfo().catch(err => { console.log(err)})
            if (proxy) {
                params.proxy.value = proxy
            }

            const key = `${window.location.pathname}Index`;
            const storedValue = Storage.get(key, true); // 获取原始值（字符串或 null）

            if (storedValue !== null && storedValue !== undefined && !isNaN(storedValue)) {
                params.cIndex.value = +Storage.get(`${window.location.pathname}Index`, true)
            }

        

            if (window.localStorage.getItem('pl')) {
                params.countSetting.value.time = window.localStorage.getItem('pl')
            } else {
                window.localStorage.setItem('pl', params.countSetting.value.otime)
                params.countSetting.value.time = params.countSetting.value.otime
            }

            if (window.localStorage.getItem('size')) {
                params.countSetting.value.size = window.localStorage.getItem('size')
            } else {
                window.localStorage.setItem('size', params.countSetting.value.osize)
                params.countSetting.value.size = params.countSetting.value.osize
            }

            params.yearList.value = getRecentYears()
            params.monthList.value = getMonthList()
            const { message } = antd
            params.messageTest.value = message 
            params.messageTest.value.config({
                top: `10vh`,
                duration: 7,
                maxCount: 1,
                rtl: true,
            });

            setTimeout(() => {
                let info = Storage.get('AccountInfo', true)
                params.userinfo.value=info
                //if (info&&info.DepartmentId == 20) {
                //    params.cIndex.value = 0
                //    //{ label: "月度考核", value: 1 }
                //    params.tabList.value = [{ label: "年度考核", value: 0 }, { label: "月度考核", value: 1 } ]

                //} else {
                //    params.cIndex.value = 0
                //    //{ label: "月度考核", value: 1 },
                //    params.tabList.value = [{ label: "年度考核", value: 0 },{ label: "月度考核", value: 1 }]

                //}




                Request({
                    url: "/Account/createJwtToken",
                    method: 'post',
                    data: {
                        "userName": info && info.UserNo
                    }
                }).then(res => {
                    Storage.set('token', res.Data,true)

                 
                    methods.getTableList(true)
                    methods.GetDepartmentList()
                    methods.GetDepartmentTypeList()
                    methods.GetStatusList()

                    document.addEventListener('visibilitychange', function () {
                        if (document.visibilityState === 'visible') {
            
                            if (params.popShow.value) {
                                methods.handleFillRecord(params.orecord.value)
                            }

                            methods.getTableList()

                        } 
                    });

                })
            },400)



        },

    }



    const template = {
        test: {
            template: `<div @click="add">+</div> <div>{{testnum}}</div>`,

            setup() {

                return {
                    ...params,
                    ...methods,
                };
            }

        }
    }
    return {
        ...params,
        ...methods,
        template
    }
}

export default Composition